<template>
<j-modal :title="title" :width="width" :visible="visible" :confirmLoading="confirmLoading" switchFullscreen @ok="handleOk" @cancel="handleCancel" cancelText="关闭">
    <a-spin :spinning="confirmLoading">
        <a-form :form="form">
            <a-row>
                <a-col :span="12">
                    <a-form-item label="生产号" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-model="dataList.productionTaskListOdd" placeholder="请输入生产号"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="仓库" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly :value="dataList.warehouse" placeholder="请输入仓库"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="领料号" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-model="dataList.odd" placeholder="请输入领料号"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="下单日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <j-date placeholder="请输入下单日期" style="width: 100%" v-decorator="['date']" :trigger-change="true" />
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="审批" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-select ref="select" v-model="dataList.state" style="width: 100%" @change="sift">
                            <a-select-option v-for="(elem,index) in options" :key="index" :value="elem.label">{{elem.label}}</a-select-option>
                        </a-select>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="核算科目" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-decorator="['accounting']" placeholder="请输入核算科目"></a-input>
                    </a-form-item>
                </a-col>
            </a-row>
        </a-form>
    </a-spin>
</j-modal>
</template>

<script>
import {
    httpAction,
} from '@/api/manage'
import pick from 'lodash.pick'
import JDate from '@/components/jeecg/JDate'
export default {
    name: "StoreCheckModal",
    components: {
        JDate
    },
    data() {
        return {
            form: this.$form.createForm(this),
            title: "操作",
            width: 800,
            visible: false,
            model: {},
            dataList: {
                state: ''
            },
            options: [{
                value: '通过',
                label: '通过'

            }, {
                value: '不通过',
                label: '不通过'

            }],
            labelCol: {
                sm: {
                    span: 6
                },
            },
            wrapperCol: {
                sm: {
                    span: 16
                },
            },
            confirmLoading: false,
            validatorRules: {},
            url: {
                edit: "/production/picking/editState",
            },
        }
    },
    methods: {
        sift(value) {
            this.$set(this.dataList, 'state', value);
        },
        add() {
            this.edit({});
        },
        edit(record) {
            if (record.state == '待审核' || record.state == '未审核' || record.state == '不通过') {
                this.form.resetFields();
                this.model = Object.assign({}, record);
                let {
                    odd,
                    state,
                    warehouse,
                    productionTaskListOdd
                } = record;
                this.dataList = {
                    odd,
                    state,
                    warehouse,
                    productionTaskListOdd,
                };
                this.visible = true;
                this.$nextTick(() => {
                    this.form.setFieldsValue(pick(this.model, 'date', 'accounting'))
                })
            }
            else if(record.state == '待领料'){
                this.$message.warning('未领料，请先领料!')
            } else {
                this.$message.warning('已审核,状态不可逆!')
            }
        },
        close() {
            this.$emit('close');
            this.visible = false;
        },
        handleOk() {
            const that = this;
            // 触发表单验证
            this.form.validateFields((err, values) => {
                if (!err) {
                    that.confirmLoading = true;
                    let httpurl = '';
                    let method = '';
                    if (this.model.id) {
                        httpurl += this.url.edit;
                        method = 'post';
                    }
                    Object.assign(values, this.dataList);
                    let formData = Object.assign(this.model, values);
                    httpAction(httpurl, formData, method).then((res) => {
                        if (res.success) {
                            that.$message.success(res.message);
                            that.$emit('ok');
                        } else {
                            that.$message.warning(res.message);
                        }
                    }).finally(() => {
                        that.confirmLoading = false;
                        that.close();
                    })
                }

            })
        },
        handleCancel() {
            this.close()
        },
        popupCallback(row) {
            this.form.setFieldsValue(pick(row, 'date', 'accounting'))
        },

    }
}
</script>
